package com.season.act;



import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

public class StartAct {
    //得到流程引擎对象
    private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();


    public void deplory(){
        //得到部署的service
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //部署
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("newAct.bpmn")//加载流程文件
                .name("worldProcess")//流程名称
                .deploy();//部署
        //得到部署的信息
        System.out.println("部署ID"+deploy.getId());
    }

    public void deployZIP(){
        //加载zip文件
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("newAct.zip");
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        //得到部署的service
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                .addZipInputStream(zipInputStream)
                .name("worldProcess2")
                .deploy();
        //得到部署的信息
        System.out.println("部署ID"+deploy.getId());

    }

    public void start(){
        //得到运行service
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //根据key(唯一ID标示)来启动流程实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess_1");
        //根据流程实例得到具体信息
        System.out.println("流程实例ID"+processInstance.getId());
        System.out.println("流程定义ID"+processInstance.getProcessDefinitionId());
    }

    public void findTask(){
        //得到任务service
        TaskService taskService = processEngine.getTaskService();
        //查询
        List<Task> list = taskService.createTaskQuery()
                .taskAssignee("攻城狮")//按照指定人的名称查询
                .list();

        for (Task task : list) {
            System.out.println("任务ID"+task.getId());
            System.out.println("任务名称"+task.getName());
            System.out.println("执行者"+task.getAssignee());
            System.out.println("流程定义ID"+task.getProcessDefinitionId());
        }
    }
    //完成任务
    public void completTask(){
        //得到任务service
        TaskService taskService = processEngine.getTaskService();

        taskService.complete("2504");
    }


    public static void main(String[] args) {
        StartAct startAct=new StartAct();
        startAct.deplory();
        //startAct.start();
        //startAct.findTask();
        //startAct.completTask();
    }
}
